package ar.edu.unicen.pladema.vc1.practico1;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import ar.edu.unicen.pladema.ui.model.ColorRange;
import ar.edu.unicen.pladema.vc1.formats.hgt.HGT;
/**
 * Pintar un rectánculo en pantalla, utilizando un degrade que vaya de negro a 
 * amarillo en la primer mitad del ancho del rectángulo, y de amarillo a rojo, 
 * en la segunda mitad. En Y, el degrade se mantiene constante.
 * Amarillo: 255, 255, 0
 * Rojo: 255,0,0
 * @author kache
 *
 */
public class P1Enunciado4Drawer implements PaintListener {
	//e.gc.drawPoint(i, j); i ancho
	private static int WIDTH=600;
	private static int HEIGHT=600;
	private Composite panel;
	private String fileDir;
	private Label limites;
	private HGT hgt=null;
	private static ColorRange cr=null;
	
	public P1Enunciado4Drawer(Composite panel) {
		this.panel = panel;
		hgt = new HGT();
		this.fileDir = "";
	}
	@Override
	public void paintControl(PaintEvent e) {
		if (! hgt.loaded()) {
			return;
		}
		if (cr == null) {
			cr = testPaleta();
		}
		
//		ColorProgress cp = new ColorProgress(new RGB(255,255,255), new RGB(0,0,0));
		System.out.println("pintando..");
		for (int i=0; i<WIDTH; i++ ) {
			for (int j=0; j<HEIGHT; j++) {
				
				int hvalue = hgt.getValue(i, j);
//				int r = hvalue / 256;
//				int g = hvalue % 255;
//				int d = 255 * (hvalue/hgt.getMaxValue());
				//new Color()
				//Color c = new Color (panel.getDisplay(), r,g, 0);
				Color c = new Color (panel.getDisplay(), cr.get(hvalue));
				
				
				e.gc.setForeground(c);
				e.gc.drawPoint(i, j);
			}
		}
		
	
	}
	public ColorRange testPaleta() {
		if (!hgt.loaded()) {
			return testPaleta2();
		}
		ColorRange testCR = new ColorRange(0, new RGB(0,0,0));
		testCR.add(64, new RGB(255,0,255));
		//cr.add(130, new RGB(255,0,0));
		testCR.add(10000, new RGB(255,255,0));
		testCR.add(30000, new RGB(0,255,0));
		testCR.add(45000, new RGB(0,255,255));
		testCR.add(65476, new RGB(0,0,255));
		testCR.add(65511, new RGB(0,25,255));
		testCR.add(hgt.getMaxValue(), new RGB(255,255,255));
		return testCR;
	}
	public ColorRange basicPaleta() {
		ColorRange testCR = new ColorRange(0, new RGB(0,0,0));
		testCR.add(66000, new RGB(255,255,255));
		return testCR;
	}
	public static ColorRange testPaleta2() {
		ColorRange testCR = new ColorRange(0, new RGB(0,0,255));
		testCR.add(10, new RGB (0, 0, 255));
		testCR.add(30, new RGB (30, 144, 255));
		testCR.add(60, new RGB (0, 255, 0));
		testCR.add(90, new RGB (255, 255, 0));
		testCR.add(120, new RGB (255, 165, 0));
		testCR.add(150, new RGB (139, 105, 20));
		testCR.add(180, new RGB (58, 43, 5));
		testCR.add(210, new RGB (0, 0, 0));
		testCR.add(240, new RGB (160, 32, 240));
		testCR.add(500, new RGB (160, 32, 240));
		testCR.add(10000, new RGB (255, 255, 0));
		testCR.add(65400, new RGB (0, 0, 255));
		testCR.add(66000, new RGB (160, 32, 240));
		return testCR;
	}
	public static ColorRange testPaleta3() {
		ColorRange testCR = new ColorRange(0, new RGB(0,0,0));
		testCR.add(15, new RGB (0, 0, 0));
		testCR.add(60, new RGB (255, 255, 0));
		testCR.add(120, new RGB (0, 255, 0));
		testCR.add(180, new RGB (255, 0, 0));
		testCR.add(230, new RGB (0, 0, 255));
		testCR.add(250, new RGB (255, 255, 255));
		testCR.add(66000, new RGB (0, 0, 0));
		return testCR;
	}
	public void setHGTFile(String file) {
		if (this.fileDir.equals(file)) {
			return;
		}
		this.fileDir = file;
		hgt.loadFromFile(fileDir);
		limites.setText(hgt.getMinValue() + ":" + hgt.getMaxValue());
	}
	public void setColorRange(ColorRange colorRange) {
		cr = colorRange;
	}
	public void setLabelInfo(Label limites) {
		this.limites = limites;
	}
	public int getLandHeight(int x, int y) {
		if (! hgt.loaded()) {
			return -1;
		}
		Integer r = hgt.getValue(x, y);
		if (r == null) {
			return -1;
		}
		return r.intValue();
	}
}
